// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   WallView.java

package jones.gui;

import java.awt.Rectangle;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;
import javax.swing.OverlayLayout;
import jones.Wall;
import jones.World;
import jones.rigid.OverlappingRigidBodyException;

// Referenced classes of package jones.gui:
//            RigidBodyView, PositionHandle, Position

public class WallView extends RigidBodyView
    implements Observer
{

    private static final int HANDLE_SIZE = 5;
    private Wall wall = null;
    private World world = null;
    private PositionHandle endPointHandle = null;
    private PositionHandle startPointHandle = null;

    public WallView(Wall wall, World world)
    {
        super(((jones.rigid.RigidBody) (wall)), world);
        this.wall = wall;
        this.world = world;
        init();
    }

    private void init()
    {
        setPositionHandlesVisible(false);
        setOrientationHandleVisible(false);
        Rectangle handleShape = new Rectangle(5, 5);
        Position endPosition = ((Position) (new Position() {

            public void setLocation(double x, double y)
            {
                wall.setEndPoint(x, y);
                wall.notifyObservers();
            }

            public double getX()
            {
                return wall.getEndPointX();
            }

            public double getY()
            {
                return wall.getEndPointY();
            }

            public void update(Observable obs, Object obj)
            {
                setChanged();
                notifyObservers();
            }

        }
));
        wall.addObserver(((Observer) (endPosition)));
        handleShape.setLocation((int)(wall.getEndPointX() - 2D), (int)(wall.getEndPointY() - 2D));
        endPointHandle = new PositionHandle(endPosition, ((java.awt.geom.RectangularShape) (handleShape)));
        Position startPosition = ((Position) (new Position() {

            public void setLocation(double x, double y)
            {
                wall.setStartPoint(x, y);
                wall.notifyObservers();
            }

            public double getX()
            {
                return wall.getStartPointX();
            }

            public double getY()
            {
                return wall.getStartPointY();
            }

            public void update(Observable obs, Object obj)
            {
                setChanged();
                notifyObservers();
            }

        }
));
        wall.addObserver(((Observer) (startPosition)));
        handleShape.setLocation((int)(wall.getStartPointX() - 2D), (int)(wall.getStartPointY() - 2D));
        startPointHandle = new PositionHandle(startPosition, ((java.awt.geom.RectangularShape) (handleShape)));
        setLayout(((java.awt.LayoutManager) (new OverlayLayout(((java.awt.Container) (this))))));
        add(((java.awt.Component) (endPointHandle)));
        add(((java.awt.Component) (startPointHandle)));
        addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent e)
            {
                startPointHandle.setVisible(true);
                endPointHandle.setVisible(true);
                repaint();
            }

            public void focusLost(FocusEvent e)
            {
                startPointHandle.setVisible(false);
                endPointHandle.setVisible(false);
                repaint();
            }

        }
);
        startPointHandle.addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

            double oldX = 0;
            double oldY = 0;

            public void mousePressed(MouseEvent e)
            {
                oldX = wall.getStartPointX();
                oldY = wall.getStartPointY();
            }

            public void mouseReleased(MouseEvent e)
            {
                if (!world.createCollisionGraph())
                {
                    JOptionPane.showMessageDialog(((java.awt.Component) (WallView.this)), "Rigid bodies cannot overlap.", "OverlappingRigidBodyException", 0);
                    wall.setStartPoint(oldX, oldY);
                    wall.notifyObservers();
                }
            }

        }
)));
        endPointHandle.addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

            double oldX = 0;
            double oldY = 0;

            public void mousePressed(MouseEvent e)
            {
                oldX = wall.getEndPointX();
                oldY = wall.getEndPointY();
                world.removeRigidBody(((jones.rigid.RigidBody) (wall)));
            }

            public void mouseReleased(MouseEvent e)
            {
                try
                {
                    world.addRigidBody(((jones.rigid.RigidBody) (wall)));
                }
                catch (OverlappingRigidBodyException ex)
                {
                    JOptionPane.showMessageDialog(((java.awt.Component) (WallView.this)), "Rigid bodies cannot overlap.", "OverlappingRigidBodyException", 0);
                    wall.setEndPoint(oldX, oldY);
                    world.addRigidBody(((jones.rigid.RigidBody) (wall)));
                    wall.notifyObservers();
                }
            }

        }
)));
    }

    public boolean contains(int x, int y)
    {
        return super.contains(x, y) || hasFocus() && endPointHandle.contains(x, y) || startPointHandle.contains(x, y);
    }




}
